<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>图书管理系统</title>
  <style type="text/css">
    .grid {
      margin: auto;
      width: 530px;
      text-align: center;
    }

    .grid table {
      border-top: 1px solid #C2D89A;
      width: 100%;
      border-collapse: collapse;
    }

    .grid th,
    td {
      padding: 10;
      border: 1px dashed #F3DCAB;
      height: 35px;
      line-height: 35px;
    }

    .grid th {
      background-color: #F3DCAB;
    }

    .grid .book {
      padding-bottom: 10px;
      padding-top: 5px;
      background-color: #F3DCAB;
    }

    .grid .total {
      height: 30px;
      line-height: 30px;
      background-color: #F3DCAB;
      border-top: 1px solid #C2D89A;
    }
  </style>
</head>

<body>
  <div id="app">
    <div class="grid">
      <div>
        <h1>图书管理</h1>
        <div class="book">
          <div>
            <label for="id">
              编号：
            </label>
            <input type="text" id="id" v-model='id' :disabled="true">
            <label for="name">
              名称：
            </label>
            <input type="text" id="name" v-model='name' @keyup.enter="handle">
            <button @click='handle' :disabled="submitFlag">提交</button>
          </div>
        </div>
      </div>
      <div class="total">
        <span>图书总数：</span>
        <span>{{books.length}}</span>
      </div>
      <table>
        <thead>
          <tr>
            <th>编号</th>
            <th>名称</th>
            <th>时间</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr :key='item.id' v-for='item in books'>
            <td>{{item.id}}</td>
            <td>{{item.name}}</td>
            <td>{{item.date | format('YYYY-MM-DD HH:mm:ss')}}</td>
            <td>
              <a href="" @click.prevent='toEdit(item.id)'>修改</a>
              <span>|</span>
              <a href="" @click.prevent='deleteBook(item.id)'>删除</a>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
  <script src="js/axios.js"></script>
  <script type="text/javascript" src="js/vue.js"></script>
  <script src="js/moment.js"></script>
  <script>
    axios.defaults.baseURL = 'http://127.0.0.1:3000/'

    axios.interceptors.request.use(function (config) {
      // 在发送请求之前做些什么
      console.log(config);
      return config;
    }, function (error) {
      // 对请求错误做些什么
      return Promise.reject(error);
    });

    axios.interceptors.response.use(function (response) {
      // 对响应数据做点什么
      return response.data;
    }, function (error) {
      // 对响应错误做点什么
      return Promise.reject(error);
    });
    Vue.directive('focus', function (e) {
      e.focus();
    })
    var vm = new Vue({
      el: '#app',
      data: {
        books: [],
        id: null,
        flag: false,
        name: '',
        submitFlag: true,
        totalBookNum: 0
      },
      methods: {
        async handle() {
          this.submitFlag = true
          if (this.flag) {
            // 书籍更新信息
            let res = await axios.put(`books/${this.id}`, {
              name: this.name
            })
            if (res.status == 200) {
              this.queryData()
            }
          } else {
            // 新增书籍
            let ret = await axios.post('books', {
              name: this.name
            })
            if (ret.status == 200) {
              this.queryData()
            }

          }
          this.name = ''
          this.id = ''
          this.submitFlag = false
          this.flag = false
        },
        async toEdit(id) {
          this.id = id;
          let res = await axios.get(`books/${id}`)
          this.name = res.name
          this.flag = true
        },
        async deleteBook(id) {
          let res = await axios.delete(`books/${id}`)
          if (res.status == 200) {
            this.queryData()
          }
        },
        async queryData() {
          this.books = await axios.get('books')
        },
        async jiaoyan() {
          // true 存在 false 不存在
          let res = await axios(`books/book/${this.name}`)
          return res.status == 1
        }
      },
      filters: {
        format(val, str) {
          return moment(val).format(str);
        }
      },
      // watch: {
      //   id: function (val, oldVal) {
      //     this.books.some(item => {
      //       return val === item.id
      //     })
      //   }
      // },
      created() {
        this.queryData()
      },
      watch: {
        name(val) {
          if (val == '')
            this.submitFlag = true
          else
            this.submitFlag = false
        }
      },
    })
  </script>
</body>

</html>